/**
 * Created by MD-3 on 2017/6/13.
 */

function openInfoWindow(id) {
    var doorId = id;
    var doorConstruct = getDoorById(doorId);
    var alarmState = doorConstruct.alarmState;
    if(!alarmState || "1"!=alarmState){
        return;
    }
    var doorMarker = doorConstruct.doorMarker;

    var infoData = getInfoData(doorId);
    var doorName = infoData.basicInfo.doorName;

    var title = createInfoTitle(doorName);

    var content = '<div style="width: 200px;height: 120px;overflow:auto;">'
        + createBasicInfo(infoData.basicInfo)
        + '</div>';
    var infoWindow = new AMap.InfoWindow({
        isCustom: true,                                              //使用自定义窗体
        content: createInfoWindow(title, content),
        offset: new AMap.Pixel(16,-16)
    });
    infoWindow.open(map, doorMarker.getPosition());
    map.panTo(doorMarker.getPosition());
}

function getInfoData(doorId){
    var infoDataList = infoWinow_mock;
    var infoData;
    for(var i in infoDataList){
        if(doorId != infoDataList[i].basicInfo.doorId){
            continue;
        }
        infoData = infoDataList[i];
    }
    return infoData;
}

function createInfoTitle(titletext) {
    return '<span class="infotitle">' + titletext + '</span>';
}

function createBasicInfo(basicInfo) {
    var doorId = basicInfo.doorId;
    var doorName = basicInfo.doorName;
    var doorState = basicInfo.doorState;
    var alarmState = basicInfo.alarmState;
    var btnValue = alarmState=="2" ? "已处理" : "处理";

    return 'ID:'+doorId+'<br/>'
        +'名称:'+doorName+'<br/>'
        +'状态:'+doorState+'<br/>'
            +'<div>'
        +'<input type="radio" name="optionsRadios" id="optionsRadios0" value="option0">确认报警'
        +'<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1">确认误报'
            +'</div>'
        +'<button type="button" class="btn btn-primary active" doorId="'+doorId+'" onclick="doorCheck(this)">'+btnValue+'</button>';
}

function doorCheck(thisitem) {
    var radioVal = $('input:radio[name="optionsRadios"]:checked').val();
    if (!radioVal) {
        alert("请处理报警！");
        return;
    }

    var thisItem = $(thisitem);
    var doorId   = thisItem.attr("doorId");

    var doorConstruct = getDoorById(doorId);
    var doorState = doorConstruct.doorState;
    var alarmState = doorConstruct.alarmState;
    var doorMarker = doorConstruct.doorMarker;
    if(alarmState && "1"==alarmState){
        alarmState = doorConstruct.alarmState = "2";
    }
    doorMarker.setIcon(getDoorIcon(doorState,alarmState));

    console.log("已处理：doorId="+doorId);
    thisItem.text("已处理");
    closeInfoWindow();
}



function openInfoWindow2(id) {
    var doorId = id;
    var doorConstruct = getDoorById(doorId);
    var doorMarker = doorConstruct.doorMarker;
    var doorState = doorConstruct.doorState;
    //var btnValue = doorState=="0" ? "开门" : "关门";
    var btnValue = "开门";

    var title = createInfoTitle("ID:"+doorId);

    var content = '<div style="width: 200px;height: 120px;overflow:auto;">'
        + '<button type="button" class="btn btn-primary active" doorId="'+doorId+'" onclick="doorStateTog(this)">'+btnValue+'</button>'
        + '</div>';
    var infoWindow = new AMap.InfoWindow({
        isCustom: true,                                              //使用自定义窗体
        content: createInfoWindow(title, content),
        offset: new AMap.Pixel(16,-16)
    });
    infoWindow.open(map, doorMarker.getPosition());
    map.panTo(doorMarker.getPosition());
}

function doorStateTog(thisitem) {
    var thisItem = $(thisitem);
    var doorId   = thisItem.attr("doorId");

    var doorConstruct = getDoorById(doorId);
    var doorState = doorConstruct.doorState;

    if(doorState=="0"){
        openDoorAjax(doorId);
        thisItem.text("关门");
    }else {
        thisItem.text("开门");
    }
    closeInfoWindow();
}












//构建自定义信息窗体
function createInfoWindow(title, content) {
    var info = document.createElement("div");
    info.className = "info";

    //可以通过下面的方式修改自定义窗体的宽高
    //info.style.width = "400px";
    // 定义顶部标题
    var top = document.createElement("div");
    var titleD = document.createElement("div");
    var closeX = document.createElement("img");
    top.className = "info-top";
    titleD.innerHTML = title;
    closeX.src = "http://webapi.amap.com/images/close2.gif";
    closeX.onclick = closeInfoWindow;

    top.appendChild(titleD);
    top.appendChild(closeX);
    info.appendChild(top);

    // 定义中部内容
    var middle = document.createElement("div");
    middle.className = "info-middle";
    middle.style.backgroundColor = 'white';
    middle.innerHTML = content;
    info.appendChild(middle);

    // 定义底部内容
    var bottom = document.createElement("div");
    bottom.className = "info-bottom";
    bottom.style.position = 'relative';
    bottom.style.top = '0px';
    bottom.style.margin = '0 auto';
    var sharp = document.createElement("img");
    sharp.src = "http://webapi.amap.com/images/sharp.png";
    bottom.appendChild(sharp);
    info.appendChild(bottom);
    return info;
}

//关闭信息窗体
function closeInfoWindow() {
    map.clearInfoWindow();
}
